//
//  PopVC.swift
//  Example
//
//  Created by remy on 2019/11/15.
//  Copyright © 2019 com.Risac. All rights reserved.
//

class PopVC: BaseVC {
    private lazy var picker: RisacPicker = {
        let view = RisacPicker(frame: CGRect(x: 0.0, y: Size.screenHeight - 280.0, width: Size.screenWidth, height: 280.0)) {
            $0.bgColor = FlatColor.carrot
        }
        view.sac.lineType = .top(size: 10.0, color: FlatColor.turquoise)
        view.titles = ["title1", "title2", "title3"]
        view.dataList = [["111", "222", "333"], ["aaa", "bbb", "ccc"]]
        let topBar = UIView(frame: CGRect(x: 0.0, y: 0.0, width: Size.screenWidth, height: 40.0), bgColor: FlatColor.midnightBlue)
        view.topBar = topBar
        view.delegate = self
        return view
    }()
    
    private lazy var pack: TablePack = TablePack(frame: self.bodyViewFrame) {
        $0.view.rowHeight = 44.0
        $0.action.heightCalculate = .none
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        RisacPop.hideAll()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        topBarView.centerItem = .text(title)
        let arr = ["Pop", "Mask", "iPad Popover", "Picker"]
        pack.action.handle(.tap, cellItemClass: MainItem.self) { [weak self] in
            guard let self = self else { return }
            switch $1.row {
            case 0:
                self.popTap()
            case 1:
                self.maskTap1()
            case 2:
                self.popoverTap()
            case 3:
                self.pickerTap()
            default:
                break
            }
        }
        pack.model.add(arr.map { MainItem($0) })
        self.view.addSubview(pack.view)
        pack.view.reloadData()
    }
    
    private func popTap() {
        RisacPop.hideAll()
        RisacPop.global.stayTime = 0.0
        RisacPop.success("success,在当前控制器view上") {
            $0.stage = .view(self.view)
            $0.imageSize = 69.0
        }
        QueueTask.main(delay: 1.0) {
            RisacPop.error("error延迟1.0s,在keyWindow上") {
                $0.stage = .window(true)
                $0.text.alignment = .left
            }.set {
                $0.top = Size.screenHeight - 200.0
                $0.left = 10.0
            }
        }
        let task = QueueTask.main(delay: 1.5) {
            RisacPop.info("info延迟1.5s,立即取消延迟,在newWindow上").set {
                $0.top = 180.0
                $0.left = 150.0
            }
        }
        task.cancel(true)
        QueueTask.main(delay: 2.0) {
            RisacPop.toast("toast延迟2.0s,在newWindow上") {
                $0.viewInset.right = 30.0
            }.set {
                $0.top = 180.0
                $0.left = 10.0
            }
        }
        let pop = RisacPop.loading("loading3.0s后关闭,在newWindow上")
        pop.set {
            $0.top = Size.screenHeight - 320.0
            $0.left = 200.0
        }
        QueueTask.main(delay: 3.0) {
            pop.hide()
        }
    }
    
    private func maskTap1() {
        let view = RisacView(frame: CGRect(x: 0.0, y: Size.screenHeight - 280.0, width: Size.screenWidth, height: 280.0), bgColor: FlatColor.carrot)
        view.sac.lineType = ViewLineType.bottom(UIEdgeInsets(top: 0.0, left: 16.0, bottom: 16.0, right: 16.0), size: 40.0, color: FlatColor.belizeHole).left(size: 20.0, color: FlatColor.greenSea).right(size: 20.0)
        let label = UILabel(frame: CGRect(x: 16.0, y: 50.0, width: Size.screenWidth - 32.0, height: 50.0), text: "在当前控制器view上", textColor: .white, fontSize: 15.0, bold: true, bgColor: FlatColor.midnightBlue, alignment: .center)
        label.isUserInteractionEnabled = true
        label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.maskTap2)))
        view.addSubview(label)
        RisacMask.show(view: view) {
            $0.stage = .view(self.view)
        }
    }
    
    @objc private func maskTap2() {
        let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: Size.screenWidth, height: 320.0), bgColor: FlatColor.carrot)
        let label1 = UILabel(frame: CGRect(x: 16.0, y: 50.0, width: Size.screenWidth - 32.0, height: 50.0), text: "在keyWindow上", textColor: .white, fontSize: 15.0, bold: true, bgColor: FlatColor.midnightBlue, alignment: .center)
        view.addSubview(label1)
        let label2 = UILabel(frame: CGRect(x: 16.0, y: label1.bottom + 16.0, width: Size.screenWidth - 32.0, height: 50.0), text: "点击popVC,不关闭RisacMask,测试泄露", textColor: .white, fontSize: 15.0, bold: true, bgColor: FlatColor.midnightBlue, alignment: .center)
        view.addSubview(label2)
        label2.isUserInteractionEnabled = true
        label2.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.maskTap3)))
        RisacMask.show(view: view) {
            $0.stage = .window(false)
        }
    }
    
    @objc private func maskTap3() {
        self.popVC()
    }
    
    @objc private func popoverTap() {
        let vc = UIAlertController(title: "ActionSheet", message: nil, preferredStyle: .actionSheet)
        vc.addAction(UIAlertAction(title: "action1", style: .default))
        vc.addAction(UIAlertAction(title: "action2", style: .default))
        vc.addAction(UIAlertAction(title: "取消", style: .cancel))
        self.modal(vc)
    }
    
    private func pickerTap() {
        picker.selectRow(1, inComponent: 0)
        picker.selectRow(2, inComponent: 1)
        RisacMask.show(view: picker)
    }
}

extension PopVC: RisacPickerDelegate {
    func pickerDidSelected(picker: RisacPicker, indexPath: IndexPath) {
        Risac.print(indexPath)
    }
}
